# Replication materials for Figures 1 and 2 from Banda and Windett's
# "Negative Advertising and the Dynamics of Candidate Support"

# Load gplots library
library(gplots)

# This opens the polling data, saved as a CSV. Place in the same directory as the script file.
fig<-read.csv("Polling Graphics.csv")
attach(fig)
term=WeekTilElection
term2=WeekOfElection
term3=WeekOut
SCO=SmoothCO
ACO=AverageCO
SGA=SmoothGA
AGA=AverageGA
CORaw=D2PPct



# Creates both panels of Figure 1, saves as PDF documents in your working directory.
pdf("GA2.pdf",
    height=5.5, width=5.5)
plot(term, SGA,type="n", ylim = c(40, 75), xlim=c(12,1) ,col = "gray25",lty=2, lwd = 3, axes = FALSE, xlab = "", ylab = "", cex.main = 1.) # Use type = "n" to get the plot defined
lines(term, SGA, col="black", lty=1, lwd=3)
points(term, AGA, col = "gray25",lty=3, lwd = 1, pch=16) 
axis(1, at = seq(1, 12, by = 1), las = 1)
axis(2, at = seq(40, 75, by = 5), las = 1)
box()
title(xlab = "Weeks Until Election Day", line = 3.5, cex.lab = 1.25)
title(ylab = "Democratic Two Party Vote Share", line = 3, cex.lab = 1.25)
legend("bottomright", bty = "n", ncol = 1,lty=c(1,0),lwd=c(3,NA), pch= c(NA,16), c("Smooth Polling", "Raw Polling Averages"))
title(main="2000 Georgia Senate Election")
dev.off()

pdf("CO2.pdf",
    height=5.5, width=5.5)
plot(term, SCO,type="n", ylim = c(40, 75), xlim=c(12,1) ,col = "gray25",lty=2, lwd = 3, axes = FALSE, xlab = "", ylab = "", cex.main = 1.) # Use type = "n" to get the plot defined
lines(term, SCO, col="black", lty=1, lwd=3)
points(term3, CORaw, col = "gray25",lty=1, pch=16) 
axis(1, at = seq(1, 12, by = 1), las = 1)
axis(2, at = seq(40, 75, by = 5), las = 1)
box()
title(xlab = "Weeks Until Election Day", line = 3.5, cex.lab = 1.25)
title(ylab = "Democratic Two Party Vote Share", line = 3, cex.lab = 1.25)
legend("bottomright", bty = "n", ncol = 1,lty=c(1,0), lwd=c(3, NA), pch= c(NA,16), c("Smooth Polling", "Raw Polling"))
title(main="2004 Colorado Senate Election")
dev.off()

# Code to replicate Figure 2

# Left panel
# Effects of Democratic Attack Advantage on Democratic Polling Advantage
# From equation predicting Democratic support
# coef of lagged attack ads difference
lag.att <- -.0030373
# coef of first difference of attack ads difference
dif.att <- .007596
# coef of lagged Democratic support
lag.yatt<- -.0300379
# standard deviation of attack ads difference
sd.att  <- 34.19231

# Attacks
a<-dif.att*sd.att
b<-(lag.att-dif.att)*sd.att+((1-abs(lag.yatt))*a)
c<-(1-abs(lag.yatt))*b
d<-(1-abs(lag.yatt))*c
e<-(1-abs(lag.yatt))*d

attack<-as.vector(c(a,b,c,d,e))
names(attack)<-list('0','1','2','3','4')

# Distributed lag plots
pdf("DistributedEffectsDiff.pdf",
    height=5.5, width=5.5)
barplot2(attack, xlab = "Weeks after change", ylab = "Change in Democratic Polling Advantage",
         main='Effects of Democratic Attack Advantage \non Democratic Polling Advantage',ylim=c(-.12,.26),col='gray80',
         plot.grid=T,grid.col='gray46',grid.inc=14)
dev.off()

# Right panel
# Effects of Democratic Polling Advantage on Democratic Attack Advantage
# From equation predicting attack ad difference
# coef of lagged Democratic support
lag.sup <- -.2694146
# coef of first difference of Democratic support
dif.sup <- 1.046692
# coef of lagged attack ads difference
lag.ysup<- -.4486273
# standard deviation of Democratic support
sd.sup  <- 8.899016

# Attacks
a1<-dif.sup*sd.sup
b1<-(lag.sup-dif.sup)*sd.sup+((1-abs(lag.ysup))*a1)
c1<-(1-abs(lag.ysup))*b1
d1<-(1-abs(lag.ysup))*c1
e1<-(1-abs(lag.ysup))*d1

support<-as.vector(c(a1,b1,c1,d1,e1))
names(support)<-list('0','1','2','3','4')

# Distributed lag plots
pdf("DistributedEffectsSupp.pdf",
    height=5.5, width=5.5)
barplot2(support, xlab = "Weeks after change", ylab = "Change in Democratic Attack Advantage",
         main='Effects of Democratic Polling Advantage \non Democratic Attack Advantage',ylim=c(-8,10),col='gray80',
         plot.grid=T,grid.col='gray46',grid.inc=10)
dev.off()


